package com.cvit.service;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cvit.dao.CallbackMapper;
import com.cvit.dao.OrdersMapper;
import com.cvit.model.Callback;
import com.cvit.model.ExcelBean;
import com.cvit.model.Orders;
import com.cvit.utils.ExcelUtil;
import com.cvit.utils.Utils;

@Service
public class OrdersService {
	
	@Autowired
	OrdersMapper ordersMapper;
	@Autowired
	CallbackMapper callbackMapper;

	/**
	 * 查询所有
	 * */
	public List<Orders> getAll() {
		List<Orders> orders = ordersMapper.selectAll();
		return orders;
	}

	/**
	 * 根据考生号、身份证号查询
	 * */
	public List<Orders> getOrdersBys(Orders orders) {
		List<Orders> ordersL = ordersMapper.selectOrdersBys(orders);
		return ordersL;
	}
	
	/**
	 * 根据学校名称和专业名称查询
	 * */
	public List<Orders> selectOrdersBySchoolAndMajor(Orders record){
		List<Orders> ordersSchool = ordersMapper.selectOrdersBySchoolAndMajor(record);
		return ordersSchool;
	}
	
	/**
	 * 根据订单号查询
	 * */
	public String selectOpenid(String id){
		String openid = ordersMapper.selOpenid(id);
		System.out.println("opid"+openid);
		return openid;
	}
	/**
	 * 添加订单
	 * */
	public void addOrders(Orders orders){
		orders.setPayType(2);
		ordersMapper.insert(orders);
		
	} 
	
	/**
	 * 修改缴费状态
	 * */
	public int updatePayType(String id){
		int i=ordersMapper.updatepayTypeById(id);
		System.out.println("支付状态:"+i);
		return i;
	}
	
	/**
	 * 导出Excel
	 * */
	public XSSFWorkbook exportExcelInfo(String ordersData) throws InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {  
	    //根据条件查询数据，把数据装载到一个list中  
		List<Orders> orders = ordersMapper.selectAll();
	    List<ExcelBean> excel=new ArrayList<>();
	    Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
	    XSSFWorkbook xssfWorkbook=null;
	    //设置标题栏
	    excel.add(new ExcelBean("考生号","examineeId",0));
	    excel.add(new ExcelBean("身份证号","cardId",0));
	    excel.add(new ExcelBean("姓名","name",0));
	    excel.add(new ExcelBean("报考学校","school",0));
	    excel.add(new ExcelBean("填报专业","major",0));
	    excel.add(new ExcelBean("价格","price",0));
	    excel.add(new ExcelBean("时间","startTime",0));
	    map.put(0, excel);
	    String sheetName = ordersData;
	    //调用ExcelUtil的方法
	    xssfWorkbook = ExcelUtil.createExcelFile(Orders.class, orders, map, sheetName);
	    return xssfWorkbook;
	}
	
	/**
	 * 导出Excel-School and major(school和major可以为空)
	 * */
	public XSSFWorkbook exportExcelInfoBySchoolAndMajor(String ordersData,Orders order) throws InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {  
	    //根据条件查询数据，把数据装载到一个list中
		List<Orders> orders = selectOrdersBySchoolAndMajor(order);
		System.out.println(orders);
	    List<ExcelBean> excel=new ArrayList<>();
	    Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
	    XSSFWorkbook xssfWorkbook=null;
	    //设置标题栏
	    excel.add(new ExcelBean("考生号","examineeId",0));
	    excel.add(new ExcelBean("身份证号","cardId",0));
	    excel.add(new ExcelBean("姓名","name",0));
	    excel.add(new ExcelBean("报考学校","school",0));
	    excel.add(new ExcelBean("填报专业","major",0));
	    excel.add(new ExcelBean("价格","price",0));
	    excel.add(new ExcelBean("时间","startTime",0));
	    map.put(0, excel);
	    String sheetName = ordersData;
	    //调用ExcelUtil的方法
	    xssfWorkbook = ExcelUtil.createExcelFile(Orders.class, orders, map, sheetName);
	    return xssfWorkbook;
	}
	/**
	 * Id判重
	 * */
	public String JudgeIdRepeat(){
		String id=null;
		int i=0;
		for(;i==0;){
			String orderNo = "06";
			SimpleDateFormat sdf=new SimpleDateFormat("yyMMdd");
			String nowTime = sdf.format(new Date())+Utils.get6Radom();
			orderNo+=nowTime;
			id = orderNo;
			if(ordersMapper.selectByPrimaryKey(id)==null){
				i=1;
			}
		}
		return id;
	}
	/**
	 * 凭证（根据学生表Id）
	 * */
	public Orders getVoucher(String studentId) {
		// TODO Auto-generated method stub
		Orders orders = ordersMapper.selectByOpenId(studentId);
		return orders;
	}
	
	/**
	 * @author WYHY
	 * @param 记录微信返回订单 
	 * */
	public int insCallback(Callback callback) {
		// TODO Auto-generated method stub
		int i=callbackMapper.insert(callback);
		return i;
	}
}
